#!/usr/bin/python
# -*- coding: utf-8 -*-
import platform
import Queue


__author__ = 'xiwei'

# def test or release
test = True

# configure for app version
top_level = 1
sub_level = 2
release = 4


version_suffix = ''
conf_version_require = 0

# configure for changelog template
changelog_template = u"""
版本:%(version)s,发布日期:%(release_date)s
更新信息:
%(summary)s
"""

# configure for opends api url
opends_api_url_prefix = 'https://open.bdp.cn/api'

# configure for client service or upgrade url
client_service_url_prefix = 'https://update.bdp.cn'
upgrade_url = 'https://update.bdp.cn'

if test:
    top_level = 1
    sub_level = 2
    release = 13

    version_suffix = '-offline'
    opends_api_url_prefix = 'http://dev01.haizhi.com:12121/api'
    # opends_api_url_prefix = 'http://123.126.105.35:2470/api'
    client_service_url_prefix = 'http://101.201.140.132:19987'
    upgrade_url = 'http://101.201.140.132:19987'

version = "%s.%s.%s" % (top_level, sub_level, release)


runtime_path = 'runtime'
cache_path = 'cache'

# configure for global http/https request header
headers = {
    'Content-type': 'text/html;charset=utf-8',
    'Content-Encoding': 'gzip',
    'User-Agent': 'bdp-sync/%s%s/%s' % (version, version_suffix, platform.platform())
}


# configure for global message queue
def singleton(cls, *args, **kw):
    instances = {}

    def _singleton():
        if cls not in instances:
            instances[cls] = cls(*args, **kw)
        return instances[cls]

    return _singleton


@singleton
class GlobalSignal:

    # Signal type
    SYNC_STATE = 0
    SYNC_LOG = 1

    SYNC_TABLE_STATE = 10

    runtime = {}

    def __init__(self):
        self.signal_queue = Queue.Queue()

        try:
            from PyQt4.QtCore import Qt
            self.STATE_SUCCESS_COLOR = Qt.green
            self.STATE_FAILED_COLOR = Qt.red
            self.STATE_SYNCING_COLOR = Qt.blue
            self._has_qt = True

            self.COLOR_MAP = {int(Qt.green): Qt.green, int(Qt.red): Qt.red, int(Qt.blue): Qt.blue}
        except ImportError:
            self.STATE_SUCCESS_COLOR = 0
            self.STATE_FAILED_COLOR = 1
            self.STATE_SYNCING_COLOR = 2
            self._has_qt = False

            self.COLOR_MAP = {0: 0, 1: 1, 2: 2}

    def send_signal(self, m_type, m_content):
        if self._has_qt:
            self.signal_queue.put((m_type, m_content))

    def get_signal(self):
        if self._has_qt:
            try:
                message = self.signal_queue.get()
                return message
            except Exception, e:
                raise e

    def sync_table_state(self, database, table, state):
        self.send_signal(self.SYNC_TABLE_STATE, {'database': database, 'table': table, 'state': state})


g = GlobalSignal()
